// pages/classify/index/index.js
const api = require('../../../api/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navList: [],
    categoryList: [],
    currentCategory: {},
    scrollLeft: 0,
    scrollTop: 0,
    scrollHeight: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getClassify();
  },

  getClassify: function () {
    let that = this;
    wx.showLoading({
      title: '加载中...',
    });
    that.getCurrentCategory (0);
  },

  getCurrentCategory: function (id) {
    let that = this;
    wx.request({
      url: api.ClassifyCategory,
      data: {
        page: 1,
        per_page: 8,
        son: id
      },
      method: "POST",
      header: {
        "content-type" : "application/json"
      },
      success: function (res) {
        if (res.data.error_code == 0) {
          if (id == 0) {
            that.setData({
              navList: res.data.categories,
              currentCategory: res.data.categories[0].categories,
              CategoryId: res.data.categories[0].id,
            });
          } else {
            that.setData({
              currentCategory: res.data.categories,
              CategoryId: id,
            });
          }
          wx.hideLoading();
        }
      }
    })
  },

  getList: function () {
    var that = this;
    wx.request({
      url: api.API_HOST + 'api/catalog/' + that.data.currentCategory.cat_id,
      success: function (res) {
        that.setData({
          categoryList: res.data,
        });
      },
    })
  },

  switchCate: function (event) {
    var that = this;
    var currentTarget = event.currentTarget;
    if (this.data.currentCategory.id == event.currentTarget.dataset.id) {
      return false;
    }
    this.getCurrentCategory(event.currentTarget.dataset.id);
  },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})